package NC;

public class NC49 {
    public int longestValidParentheses (String s) {
        // write code here
        int right = s.length()-1;
        int left = 0;
        boolean leftflag=false,rightflag=false;
        while (left<right){
            if(s.indexOf(left)!='('&&!leftflag){
                left++;
            }else {
                leftflag =true;
            }
            if(s.indexOf(right)!=')'&&!rightflag){
                right--;
            }else {
                rightflag =true;
            }
        }
        if(left==right) return 0;
        else return right-left;

    }
}
